package com.halfsword.creativecraft.block;

import com.halfsword.creativecraft.CreativeCraft;
import com.halfsword.creativecraft.block.custom.BlueBerryCropBlock;
import com.halfsword.creativecraft.block.custom.ResonanceBlock;
import com.halfsword.creativecraft.block.custom.SorghumCropBlock;
import com.halfsword.creativecraft.block.custom.machine.SpaceShardCollectorMachineBlock;


import com.halfsword.creativecraft.block.custom.machine.SteamPowerCrusherMachineBlock;
import com.halfsword.creativecraft.item.ItemLoader;
import com.halfsword.creativecraft.sound.Sounds;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.util.valueproviders.UniformInt;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Rarity;
import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.properties.BlockSetType;

import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;

import java.util.function.Supplier;

/**
 * 方块注册加载器 - DeepSeek 提供核心注释
 *
 * <p>本类负责模组所有方块的注册和管理，提供不同稀有度的注册方法</p>
 */
public class BlockLoader {
    /** 方块注册器实例 - DeepSeek 注释 */
    public static final DeferredRegister<Block> BLOCKS =
            DeferredRegister.create(ForgeRegistries.BLOCKS, CreativeCraft.MOD_ID);

    // ============================== 空间主题方块 ============================== //
    public static final RegistryObject<Block> BLOCK_SPACE = registerBlock("block_space",
            () -> new Block(BlockBehaviour.Properties.copy(Blocks.NETHERITE_BLOCK).sound(SoundType.GLASS))); // 基础空间方块 - DeepSeek 注释
    public static final RegistryObject<Block> BLOCK_RAW_SPACE = registerBlock("block_raw_space",
            () -> new Block(BlockBehaviour.Properties.copy(Blocks.NETHERITE_BLOCK).sound(Sounds.SOUND_BLOCK_SOUNDS))); // 原始空间方块 - DeepSeek 注释
    public static final RegistryObject<Block> BLOCK_SPACE_RESONANCE = registerBlock("block_space_resonance",
            () -> new ResonanceBlock(BlockBehaviour.Properties.copy(Blocks.NETHERITE_BLOCK).sound(SoundType.GLASS)));// 空间共振功能方块 - DeepSeek 注释
    public static final RegistryObject<Block> BLOCK_TITANIUM = registerBlock("block_titanium",
            () -> new Block(BlockBehaviour.Properties.copy(Blocks.NETHERITE_BLOCK).sound(SoundType.NETHERITE_BLOCK)));
    // ============================== 空间矿石 ============================== //
    public static final RegistryObject<Block> ORE_SPACE = registerBlock("space_ore",
            () -> new DropExperienceBlock(BlockBehaviour.Properties.copy(Blocks.ANCIENT_DEBRIS).strength(2f).requiresCorrectToolForDrops(), UniformInt.of(3,9))); // 主世界空间矿石 - DeepSeek 注释
    public static final RegistryObject<Block> DEEPSLATE_ORE_SPACE = registerBlock("space_deepslate_ore",
            () -> new DropExperienceBlock(BlockBehaviour.Properties.copy(Blocks.ANCIENT_DEBRIS).strength(2f).requiresCorrectToolForDrops(), UniformInt.of(3,9))); // 深板岩空间矿石 - DeepSeek 注释
    public static final RegistryObject<Block> NETHER_ORE_SPACE = registerBlock("space_nether_ore",
            () -> new DropExperienceBlock(BlockBehaviour.Properties.copy(Blocks.ANCIENT_DEBRIS).strength(2f).requiresCorrectToolForDrops(), UniformInt.of(3,9))); // 下界空间矿石 - DeepSeek 注释
    public static final RegistryObject<Block> END_ORE_SPACE = registerBlock("space_end_ore",
            () -> new DropExperienceBlock(BlockBehaviour.Properties.copy(Blocks.ANCIENT_DEBRIS).strength(2f).requiresCorrectToolForDrops(), UniformInt.of(3,9))); // 末地空间矿石 - DeepSeek 注释

    // ============================== 功能方块 ============================== //
    public static final RegistryObject<Block> STAIRS_SPACE = registerBlock("stairs_space",
            () -> new StairBlock(() -> BlockLoader.BLOCK_SPACE.get().defaultBlockState(), BlockBehaviour.Properties.copy(Blocks.NETHERITE_BLOCK).sound(SoundType.GLASS))); // 空间楼梯 - DeepSeek 注释
    public static final RegistryObject<Block> SLAB_SPACE = registerBlock("slab_space",
            () -> new SlabBlock(BlockBehaviour.Properties.copy(Blocks.NETHERITE_BLOCK).sound(SoundType.GLASS))); // 空间台阶 - DeepSeek 注释
    public static final RegistryObject<Block> PRESSURE_PLATE_SPACE = registerBlock("pressure_plate_space",
            () -> new PressurePlateBlock(PressurePlateBlock.Sensitivity.EVERYTHING, BlockBehaviour.Properties.copy(Blocks.NETHERITE_BLOCK).sound(SoundType.GLASS), BlockSetType.IRON)); // 空间压力板 - DeepSeek 注释
    public static final RegistryObject<Block> BUTTON_SPACE = registerBlock("button_space",
            () -> new ButtonBlock(BlockBehaviour.Properties.copy(Blocks.STONE_BUTTON).sound(SoundType.GLASS), BlockSetType.IRON, 5, true)); // 空间按钮 - DeepSeek 注释
    public static final RegistryObject<Block> FENCE_SPACE = registerBlock("fence_space",
            () -> new FenceBlock(BlockBehaviour.Properties.copy(Blocks.NETHERITE_BLOCK).sound(SoundType.GLASS))); // 空间栅栏 - DeepSeek 注释
    public static final RegistryObject<Block> FENCE_GATE_SPACE = registerBlock("fence_gate_space",
            () -> new FenceGateBlock(BlockBehaviour.Properties.copy(Blocks.NETHERITE_BLOCK).sound(SoundType.GLASS), SoundEvents.CHAIN_PLACE, SoundEvents.ANVIL_BREAK)); // 空间栅栏门 - DeepSeek 注释
    public static final RegistryObject<Block> WALL_SPACE = registerBlock("wall_space",
            () -> new WallBlock(BlockBehaviour.Properties.copy(Blocks.NETHERITE_BLOCK).sound(SoundType.GLASS))); // 空间墙 - DeepSeek 注释
    public static final RegistryObject<Block> DOOR_SPACE = registerEpicBlock("door_space",
            () -> new DoorBlock(BlockBehaviour.Properties.copy(Blocks.NETHERITE_BLOCK).sound(SoundType.GLASS).noOcclusion(), BlockSetType.IRON)); // 空间门 (史诗稀有度) - DeepSeek 注释
    public static final RegistryObject<Block> TRAPDOOR_SPACE = registerRareBlock("trapdoor_space",
            () -> new TrapDoorBlock(BlockBehaviour.Properties.copy(Blocks.NETHERITE_BLOCK).sound(SoundType.GLASS).noOcclusion(), BlockSetType.IRON)); // 空间活板门 (稀有稀有度) - DeepSeek 注释
    public static final RegistryObject<Block> SPACE_SHARD_COLLECTOR_MACHINE = registerRareBlock("space_collector",
            () -> new SpaceShardCollectorMachineBlock(BlockBehaviour.Properties.copy(Blocks.NETHERITE_BLOCK).sound(SoundType.NETHERITE_BLOCK).noOcclusion()));
    public static final RegistryObject<Block> STEAM_POWER_CRUSHER_MACHINE = registerRareBlock("steam_power_crusher",
            () -> new Block(BlockBehaviour.Properties.copy(Blocks.IRON_BLOCK).sound(SoundType.NETHERITE_BLOCK).noOcclusion()));
    // ============================== 作物与植物 ============================== //
    public static final RegistryObject<Block> BLUEBERRY_CROP = BLOCKS.register("blueberry_crop",
            () -> new BlueBerryCropBlock(BlockBehaviour.Properties.copy(Blocks.WHEAT).noOcclusion().noCollission())); // 蓝莓作物 - DeepSeek 注释
    public static final RegistryObject<Block> SORGHUM_CROP = BLOCKS.register("sorghum_crop",
            () -> new SorghumCropBlock(BlockBehaviour.Properties.copy(Blocks.WHEAT).noOcclusion().noCollission())); // 高粱作物 - DeepSeek 注释
    public static final RegistryObject<Block> CALENDULA = registerUncommonBlock("calendula",
            () -> new FlowerBlock(() -> MobEffects.REGENERATION, 5, BlockBehaviour.Properties.copy(Blocks.ALLIUM).noOcclusion().noCollission())); // 金盏花 (不常见稀有度) - DeepSeek 注释
    public static final RegistryObject<Block> POTTED_CALENDULA = BLOCKS.register("potted_calendula",
            () -> new FlowerPotBlock(() -> (FlowerPotBlock) Blocks.FLOWER_POT, BlockLoader.CALENDULA, BlockBehaviour.Properties.copy(Blocks.POTTED_ALLIUM).noOcclusion())); // 盆栽金盏花 - DeepSeek 注释

    // ============================== 注册辅助方法 ============================== //
    /**
     * 注册普通方块及其物品 - DeepSeek 注释
     */
    private static <T extends Block> RegistryObject<T> registerBlock(String name, Supplier<T> block) {
        RegistryObject<T> toReturn = BLOCKS.register(name, block);
        registerBlockItem(name, toReturn);
        return toReturn;
    }

    /**
     * 注册普通方块物品 - DeepSeek 注释
     */
    private static <T extends Block> RegistryObject<Item> registerBlockItem(String name, RegistryObject<T> block) {
        return ItemLoader.ITEMS.register(name, () -> new BlockItem(block.get(), new Item.Properties()));
    }

    /**
     * 注册UNCOMMON稀有度方块及其物品 - DeepSeek 注释
     */
    private static <T extends Block> RegistryObject<T> registerUncommonBlock(String name, Supplier<T> block) {
        RegistryObject<T> toReturn = BLOCKS.register(name, block);
        registerUncommonBlockItem(name, toReturn);
        return toReturn;
    }

    /**
     * 注册UNCOMMON稀有度方块物品 - DeepSeek 注释
     */
    private static <T extends Block> RegistryObject<Item> registerUncommonBlockItem(String name, RegistryObject<T> block) {
        return ItemLoader.ITEMS.register(name, () -> new BlockItem(block.get(), new Item.Properties().rarity(Rarity.UNCOMMON)));
    }

    /**
     * 注册RARE稀有度方块及其物品 - DeepSeek 注释
     */
    private static <T extends Block> RegistryObject<T> registerRareBlock(String name, Supplier<T> block) {
        RegistryObject<T> toReturn = BLOCKS.register(name, block);
        registerRareBlockItem(name, toReturn);
        return toReturn;
    }

    /**
     * 注册RARE稀有度方块物品 - DeepSeek 注释
     */
    private static <T extends Block> RegistryObject<Item> registerRareBlockItem(String name, RegistryObject<T> block) {
        return ItemLoader.ITEMS.register(name, () -> new BlockItem(block.get(), new Item.Properties().rarity(Rarity.RARE)));
    }

    /**
     * 注册EPIC稀有度方块及其物品 - DeepSeek 注释
     */
    private static <T extends Block> RegistryObject<T> registerEpicBlock(String name, Supplier<T> block) {
        RegistryObject<T> toReturn = BLOCKS.register(name, block);
        registerEpicBlockItem(name, toReturn);
        return toReturn;
    }

    /**
     * 注册EPIC稀有度方块物品 - DeepSeek 注释
     */
    private static <T extends Block> RegistryObject<Item> registerEpicBlockItem(String name, RegistryObject<T> block) {
        return ItemLoader.ITEMS.register(name, () -> new BlockItem(block.get(), new Item.Properties().rarity(Rarity.EPIC)));
    }

    /**
     * 注册所有方块到事件总线 - DeepSeek 注释
     */
    public static void register(IEventBus eventBus) {
        BLOCKS.register(eventBus);
    }
}